﻿
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
using static UnityEngine.GraphicsBuffer;

namespace lights.polytool.editor
{
    public class GizmoTool
    {
        public static void DrawGizmoElem(comp_quickmesh_elem comp, bool withparent, bool withchild)
        {
            var parent = GetParent(comp);
            {
                var elem = parent as comp_quickmesh_elem;
                if (elem != null)
                    DrawGizmoElem(elem, true, false);


                var root = parent as comp_quickmesh;
                if (root != null)
                {
                    DrawGizmoRoot(root, false);
                }


            }
            if (withchild)
            {
                var elems = comp.GetComponentsInChildren<comp_quickmesh_elem>();
                foreach (Transform t in comp.transform)
                {
                    var elem = t.GetComponent<comp_quickmesh_elem>();
                    foreach (var e in elems)
                    {
                        DrawGizmoElem(e, false, false);
                    }
                }
            }
            Handles.color = new Color(0.3f, 1.0f, 0.3f, 0.8f);
            var ppos = parent.transform.position;
            float thick = 3.0f;
            Handles.DrawLine(ppos, comp.transform.position, thick);
        }
        public static MonoBehaviour GetParent(comp_quickmesh_elem comp)
        {
            var parent = comp.transform.parent;
            while (parent != null)
            {
                var elem = parent.GetComponent<comp_quickmesh_elem>();
                if (elem != null)
                    return elem;


                var root = parent.GetComponent<comp_quickmesh>();
                if (root != null)
                {
                    return root;
                }

                parent = parent.parent;
            }
            return null;
        }
        public static void DrawGizmoRoot(comp_quickmesh target, bool allline)
        {
            //draw a cross

            var p = target.transform.position;
            {
                Handles.color = new Color(1, 1, 1, 0.5f);
                float thick = 3.0f;
                Handles.DrawLine(p + Vector3.left * 2, p + Vector3.right * 2, thick);
                Handles.DrawLine(p + Vector3.forward * 1.25f, p + Vector3.back * 2f, thick);
                var p0 = p - Vector3.left + Vector3.forward;
                var p1 = p + Vector3.left + Vector3.forward;
                var p2 = p + Vector3.left - Vector3.forward;
                var p3 = p - Vector3.left - Vector3.forward;
                Handles.DrawLine(p0, p1, thick);
                Handles.DrawLine(p1, p2, thick);
                Handles.DrawLine(p2, p3, thick);
                Handles.DrawLine(p3, p0, thick);

                var pt0 = p + Vector3.forward * 2.5f;
                var pt1 = p + Vector3.left + Vector3.forward * 1.5f;
                var pt2 = p - Vector3.left + Vector3.forward * 1.5f;
                Handles.DrawLine(pt0, pt1, thick);
                Handles.DrawLine(pt0, pt2, thick);
                Handles.DrawLine(pt2, pt1, thick);
            }

            if (allline)
            {
                var elems = target.GetComponentsInChildren<comp_quickmesh_elem>();
                foreach (var e in elems)
                {
                    DrawGizmoElem(e, false, false);
                }
            }
        }
    }
}
